# -*- coding:utf8 -*-

import importlib, re, sys, json, datetime, random, time
from scrapy.selector import Selector
from scrapy.exceptions import CloseSpider
import urllib
import lxml.html

try:
    from scrapy.spiders import Spider
except:
    from scrapy.spider import BaseSpider as Spider

from scrapy.http import Request, FormRequest
from scrapy.utils.response import get_base_url
from scrapy.utils.url import urljoin_rfc
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from gaokaopai.items import *
from gaokaopai.dao import *

importlib.reload(sys)


class ScoreListSpider(Spider):
    name = 'score_list'
    allow = ['wmzy.com']

    def __init__(self, *args, **kwargs):
        super(ScoreListSpider, self).__init__(*args, **kwargs)

    def start_requests(self):
        url = 'https://www.wmzy.com/api/school-00-0-0-0'
        #  url = 'https://www.wmzy.com/api/school-score/twupi6.html'
        driver = webdriver.Chrome()
        driver.get(url)
        """
        driver.find_element_by_id("login_link").click()
        driver.find_element_by_id("switchForm").click()

        driver.find_element_by_id("mobile").send_keys('15205515632')
        driver.find_element_by_id("password").send_keys('wangmingzhu')
        try:
            submitBtnEl = driver.find_element_by_class_name('btn-submitFrom')
            print('submit:', submitBtnEl)
            submitBtnEl.click()
        except Exception:
            print('error')
        time.sleep(10)

        cookies = driver.get_cookies()
        print('cookies',cookies)
        driver.find_element_by_id("pin_cls").click() #关闭完善资料弹窗
       
        """
        i = 1
        for a_dom in driver.find_elements_by_xpath( \
                u"//div[contains(@class, 'm-sch-name')]/h3/a"):
            url = a_dom.get_attribute("href")
            name = a_dom.get_attribute("innerText")
            print(i)
            print(url + '============number:============' + name)
        i = i + 1
        for i in range(2, 137):
            print('============number:============', i)
            driver.find_element_by_xpath(u"//div[contains(@class,'gb_page_con')]/a[contains(@class, 'gb_page_cur')]/following-sibling::a[1]").click()

            WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.XPATH, "//a[contains(@class, 'gb_page_cur')][@value = %d]" % i)))
            for a_dom in driver.find_elements_by_xpath( \
                    u"//div[contains(@class, 'm-sch-name')]/h3/a"):
                url = a_dom.get_attribute("href")
                name = a_dom.get_attribute("innerText")
                insert(name, url)
                print(url + '============number:============' + name)


        driver.close()
        driver.quit()

    def delete_string(self, str):
        return re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", str)

    def parse_detail(self, i, driver):
        for a_dom in driver.find_elements_by_xpath( \
                u"//div[contains(@class, 'm-sch-name')]/h3/a"):
            url = a_dom.get_attribute("href")
            name = a_dom.get_attribute("innerText")
            print(url + '============number:' + i + '============' + name)

            # insert(name,number)

            # yield provinceItem
        return True
